from keras.datasets import mnist

from keras import models
from keras import layers
from keras.utils import to_categorical

# 输入数据


(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255


test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') /255
print(type(test_images))
# 构建网络
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))

network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

#分类编码
print(train_labels)
train_labels = to_categorical(train_labels)
print(train_labels)
test_labels = to_categorical(test_labels)
print(test_labels)

# 拟合  （拟合过程两个数据：损失、精度）
network.fit(train_images, train_labels, epochs=5, batch_size=128)

# 测试集精度
test_loss, test_acc = network.evaluate(test_images, test_labels)
# print('test_acc:', test_acc)
# print('test_loss:', test_loss)


